﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Domain.Values
{
	using System.Collections.Generic;
	using System.Globalization;
	using Falcon.Core.Domain.Enums;
	using Falcon.Core.Domain.Repositories;
	using Falcon.Core.Extensions;
	using Falcon.Core.Validation;
	using Falcon.Core.Validators;

	public class UserMessageCreate : ValueBase<UserMessageCreate>
	{
		// private fields
		private IList<AttachmentStore> _attachments;

		// private constants
		private const string QuoteFormat = "[quote user={0}]{1}[/quote]";

		/// <summary>
		/// Initializes a new instance of the <see cref="UserMessageCreate"/> class.
		/// </summary>
		public UserMessageCreate()
		{
			this.AttachmentStoreRepository = Application.Resolve<IAttachmentStoreRepository>();

			var identity = User.Identity;

			if (identity == null)
			{
				return;
			}

			this.Attachment = new AttachmentCreate
			{
				Bind = Settings.Users.MessageAttachmentFieldKey,
				Group = Settings.Users.MessageAttachmentKey,
				TempId = identity.Id,
				UserId = identity.Id
			};

			this.Attachments = this.AttachmentStoreRepository.GetAll(new AttachmentStoreSpecification
			{
				Group = this.Attachment.Group, 
				Key = this.Attachment.TempId.ToString(CultureInfo.InvariantCulture)
			});
		}

		/// <summary>
		/// Gets or sets the attachment store repository.
		/// </summary>
		/// <value>The attachment store repository.</value>
		public IAttachmentStoreRepository AttachmentStoreRepository
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the parent id.
		/// </summary>
		/// <value>The parent id.</value>
		public int ParentId
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the name.
		/// </summary>
		/// <value>The name.</value>
		public string Name
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the title.
		/// </summary>
		/// <value>The title.</value>
		public string Title
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the body.
		/// </summary>
		/// <value>The body.</value>
		public string Body
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets a value indicating whether this <see cref="UserMessageCreate"/> is replied.
		/// </summary>
		/// <value><c>true</c> if replied; otherwise, <c>false</c>.</value>
		public bool Replied
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets a value indicating whether this <see cref="UserMessageCreate"/> is forwarded.
		/// </summary>
		/// <value><c>true</c> if forwarded; otherwise, <c>false</c>.</value>
		public bool Forwarded
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the attachment.
		/// </summary>
		/// <value>The attachment.</value>
		public AttachmentCreate Attachment
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the attachments.
		/// </summary>
		/// <value>The attachments.</value>
		public IList<AttachmentStore> Attachments
		{
			get
			{
				if (_attachments == null)
				{
					_attachments = new List<AttachmentStore>();
				}

				return _attachments;
			}
			set
			{
				_attachments = value;
			}
		}

		/// <summary>
		/// Populates the message based on mode.
		/// </summary>
		/// <param name="message">The message.</param>
		/// <param name="mode">The mode.</param>
		public void PopulateBasedOnMode(UserMessage message, UserMessageCreateMode mode)
		{
			switch (mode)
			{
				case UserMessageCreateMode.Forward:
					{
						this.ParentId = message.Id;
						this.Title = Resources.Titles.Fw.Fmt(message.Title);
						this.Body = message.Body;
						this.Forwarded = true;
					}
					break;
				default:
					{
						this.ParentId = message.Id;
						this.Name = message.Sender.Name;
						this.Title = Resources.Titles.Re.Fmt(message.Title);
						this.Body = QuoteFormat.Fmt(message.Sender.Name, message.Body);
						this.Replied = true;
					}
					break;
			}
		}

		/// <summary>
		/// Validation rules for this instance.
		/// </summary>
		/// <returns></returns>
		public override Validator<UserMessageCreate> Rules()
		{
			var rules = base.Rules();

			rules.Add(x => x.Name)
				.NotEmpty(Resources.Errors.Required.Fmt(Resources.Titles.Name));

			rules.Add(x => x.Name)
				.Custom(new UserNameMustExistValidator(), Resources.Errors.UserNameNotFound);

			rules.Add(x => x.Name)
				.Custom(new UserMessagePreferenceValidator(), Resources.Errors.UserMessageNotAuthorized.Fmt(this.Name));

			rules.Add(x => x.Title)
				.NotEmpty(Resources.Errors.Required.Fmt(Resources.Titles.Title));

			rules.Add(x => x.Body)
				.NotEmpty(Resources.Errors.Required.Fmt(Resources.Titles.Message));

			return rules;
		}

		/// <summary>
		/// Validates this instance.
		/// </summary>
		public override void Validate()
		{
			this.Rules().Validate(this);
		}
	}
}